#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    if(pid == 0)
    {
        printf("Child: sleeping for 2 seconds...\n");
        sleep(2);
        printf("Child: done\n");
        exit(0);
    }

    int status;
    while(1)
    {
        pid_t result = result = waitpid(pid, &status, WNOHANG);
        if(result == 0)
        {
            printf("Parent: child still running...\n");
            sleep(1);
        }
        else if(result == pid)
        {
            printf("Parent: child exited\n");
            break;
        }
        else
        {
            perror("waitpid");
            break;
        }
    }

    return 0;
}

